//
//  ClockTwoView.m
//  SpecialEffectsProject
//
//  Created by 蓓蕾 on 2021/11/4.
//

#import "ClockTwoView.h"

@implementation ClockTwoView

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self==[super initWithFrame:frame]) {

        [self setupSubviews];
        [self menuTimes];
        [NSTimer scheduledTimerWithTimeInterval:1 repeats:YES block:^(NSTimer * _Nonnull timer) {
             [self menuTimes];
        }];
    }
    return self;
}
-(void)setupSubviews{
    
    _clock_bg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 488/2, 485/2)];
    _clock_bg.image = [UIImage imageNamed:@"1.png"];
    [self addSubview:_clock_bg];
    
    _clock_hour = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 10, 40)];
    _clock_hour.center = CGPointMake(_clock_bg.center.x, _clock_bg.center.y);
    _clock_hour.image = [UIImage imageNamed:@"hourPointer"];
    _clock_hour.layer.anchorPoint = CGPointMake(0.5, 0.95);
    [self addSubview:_clock_hour];

    _clock_minute = [[UIImageView alloc]initWithFrame:CGRectMake(0,0, 10, 55)];
    _clock_minute.center = CGPointMake(_clock_bg.center.x, _clock_bg.center.y);
    _clock_minute.image = [UIImage imageNamed:@"minutePointer"];
    _clock_minute.layer.anchorPoint = CGPointMake(0.5, 0.95);
    [self addSubview:_clock_minute];
    
    _clock_second = [[UIImageView alloc]initWithFrame:CGRectMake(0,  0, 10, 120)];
    _clock_second.center = CGPointMake(_clock_bg.center.x, _clock_bg.center.y);
    _clock_second.layer.anchorPoint = CGPointMake(0.5, 0.65);
    _clock_second.image = [UIImage imageNamed:@"secondPointer"];
    [self addSubview:_clock_second];
}
-(void)menuTimes{
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSUInteger units = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
    NSDateComponents * components = [calendar components:units fromDate:[NSDate date]];
    CGFloat hourAngle   = (components.hour *3600 + components.minute*60 +components.second) / (12*3600.f) * 2*M_PI;
    CGFloat minuteAngle = (components.minute*60 +components.second) / 3600.f * 2*M_PI;
    CGFloat secondAngle = (components.second) / 60.f * 2*M_PI;
    self.clock_hour.transform   = CGAffineTransformMakeRotation(hourAngle);
    self.clock_minute.transform = CGAffineTransformMakeRotation(minuteAngle);
    self.clock_second.transform = CGAffineTransformMakeRotation(secondAngle);
    !self.timeValue?:self.timeValue([NSString stringWithFormat:@"%02ld:%02ld:%02ld",components.hour,components.minute,components.second]);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
